package com.lzdongrui.common.utils.config;

import com.lzdongrui.common.utils.snowid.SnowflakeIdWorker;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.web.client.RestTemplate;

/**
 * <p>
 *
 * </p>
 *
 * @author huanghemao@lzdongrui.com
 * @className BeanConfigurer
 * @since 2023-01-03
 */
@Configuration
public class BeanConfigurer {

    /**
     *<p>
     *   使用线程安全生成雪花Id
     *</p>
     * @author huanghemao@lzdongrui.com
     * @date 2023-01-03 15:01
     * @return com.lzdongrui.common.utils.snowid.SnowflakeIdWorker
    */
    @Bean
    @Primary
    public SnowflakeIdWorker snowflakeIdWorker(){
        return new SnowflakeIdWorker(1,1);
    }

    /**
     *<p>
     *   解决雪花算法返回给前端进度丢失问题
     *</p>
     * @author huanghemao@lzdongrui.com
     * @date 2023-01-03 15:00
     * @return org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer
    */
//    @Bean("jackson2ObjectMapperBuilderCustomizer")
//    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
//        return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder
//                .featuresToDisable()
//                .serializerByType(Long.class, ToStringSerializer.instance)
//                .serializerByType(Long.TYPE, ToStringSerializer.instance);
//    }


    @Bean
    public Jackson2ObjectMapperBuilderCustomizer initJackson() {
        Jackson2ObjectMapperBuilderCustomizer c = new Jackson2ObjectMapperBuilderCustomizer() {
            @Override
            public void customize(Jackson2ObjectMapperBuilder builder) {
                //自定义Long类型转换 超过12个数字用String格式返回，由于js的number只能表示15个数字
                builder.serializerByType(Long.class,new CustomLongConverter());
                builder.serializerByType(Long.TYPE,new CustomLongConverter());
//                builder.serializationInclusion(JsonInclude.Include.NON_NULL);//不包含为空的字段
//                builder.serializationInclusion(JsonInclude.Include.NON_EMPTY);//不包含空字符串字段
//                builder.indentOutput(true).dateFormat(new SimpleDateFormatmpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            }
        };
        return c;
    }


    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory){
        return new RestTemplate(factory);
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(15000);
        factory.setReadTimeout(5000);
        return factory;
    }


}
